<?php
    /**
     * Created by: shauan.
     * Date: 18/06/14 - 21:12
     */

    App::uses('AppHelper','View/Helper');
    class FuncHelper extends AppHelper {
        public $helpers = array("Html","Form");
        public $components = array("Session");

        public function btnStatus($paramStatus,$paramId)
        {
            $btnStatus = null;
            if($paramStatus == 1){
                $btnStatus = $this->Form->button('Ativo',array('type'=>'button','class'=>'btn btn-primary btn-xs ','onclick'=>"altStatus('".$this->params['controller']."',".$paramId.",1)", 'id'=>'saStatus_'.$paramId));
            }else{
                $btnStatus = $this->Form->button('Desativado',array('type'=>'button','class'=>'btn btn-danger btn-xs ','onclick'=>"altStatus('".$this->params['controller']."',".$paramId.",0)",'id'=>'saStatus_'.$paramId));
            }
            return $btnStatus;
        }

        /**
         * showMessage method
         * Formata o metodo setFlash de modo mais facil
         * @var $message String;
         * @var $other Array('type','close','title');
         */
        public function showMessage($message,$option =array()){
            $msg = '';
            if(!empty($message)){
                $btnCloseMsg = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';

                $msg = '<div class="alert alert-default">'.$btnCloseMsg.$message.'</div>';

                if((is_array($option)) && (count($option) > 0)){
                    $title	= isset($option['title']) && !empty($option['title'])? "<strong>".$option['title']."</strong>&nbsp;":'';
                    $type	= isset($option['type']) && !empty($option['type'])? $option['type']: 'warning';
                    $class	= isset($option['class']) && !empty($option['class'])? $option['class']: '';
                    $close	= isset($option['close']) && $option['close']==false? '':$btnCloseMsg;
                    $message = $close.$title.$message;
                    $msg = '<div class="alert alert-'.$type.' '.$class.'">'.$message.'</div>';
                }
            }
            return $msg;
        }

        public function money($valor=null){
            App::uses('CakeNumber', 'Utility');
            CakeNumber::addFormat('BRL',array('before'=>'','negative'=>'- ','thousands'=>'.','decimals'=>',','places'=>2,'zero'=>0));
            $valor = CakeNumber::currency($valor,'BRL');
            return $valor;
        }

        public function converteData($data=null){
            if(!empty($data)){
                return strtotime($data);
            }
            return false;
        }

        public function NomeTipo($string){
            switch($string){
                case 'D':
                    $string = "<strong class='text-danger'>Despesa</strong>";
                    break;
                case 'R':
                    $string = "<strong class='text-success'>Receita</strong>";
                    break;
            }
            return $string;
        }

        public function btnOption($paramId=null,$options=array()){
            $options = array_merge(array(
                    'edit'=>true,
                    'view'=>true,
                    'delete'=>true,
                    'size_option'=>'fa-2x'
                ),
                $options
            );

            foreach($options as $key=>$option){
                ${$key} = $option;
            }
            $html = null;
            if(!empty($paramId)){
                $html .= '<div class="btn-group nomargin">';
                if($options['edit'] == true)
                    $html .= $this->Html->link('<i class="fa fa-pencil"></i>', array('action' => 'edit', $paramId),array("data-placement"=>"top","data-toggle"=>"tooltip","class"=>"tooltips btn btn-default btn-xs",'escape'=>false,"title"=>"","data-original-title"=>"Editar Registro"));

                if($options['view'] == true)
                    $html .= $this->Html->link('<i class="fa fa-eye"></i>', array('action' => 'view', $paramId),array("data-placement"=>"top","data-toggle"=>"tooltip","class"=>"tooltips btn btn-default btn-xs",'escape'=>false,"title"=>"","data-original-title"=>"Ver Registro"));

                if($options['delete'] == true)
                    $html .= $this->Form->postLink('<i class="fa fa-trash-o"></i>', array('action' => 'delete', $paramId), array('escape'=>false,"title"=>"","data-original-title"=>"Deletar Registro","data-placement"=>"top","data-toggle"=>"tooltip","class"=>"tooltips btn btn-danger btn-xs"), __('Deseja realmente excluir este registro?'));
                $html .= '</div>';
            }
            return $html;
        }

        public function ativaContaVencida($data=null,$dataPagamento=null){
            $ativaClass = 'info';
            $dataHoje = $this->converteData(date('Y-m-d'));
            if($dataPagamento == null){
                if(!empty($data)){
                    $dataVencimento = $this->converteData($data);
                    if($dataVencimento < $dataHoje){
                        $ativaClass = 'danger';
                    }
                }
            }else{
                $ativaClass = 'success';
            }
            return $ativaClass;
        }

        public function DateFormat($value = array())
        {
            $date = '';
            $return_date = '';

            if(!empty($value) && is_array($value))
            {
                if(!empty($value['date'])){
                    if($value['time'] == 1){
                        list($date,$time) = explode(' ',$value['date']);
                    }else{
                        $date = $value['date'];
                    }
                    $separator = isset($value['separator']);
                    $separator = empty($separator)? '-' : $separator;
                    list($year,$months,$day) = explode($separator,$date);
                    $day = $day <= 31 ? $day : 00;
                    $months = $months <= 12 ? $months : 00;
                    $year = strlen($year) == 4 || strlen($year) == 2  ? $year : 0000;
                    if(($day == 00) || ($months == 00) || ($year == 0000)){
                        $return_date = '';
                    }else{
                        $months_ext = array(array('0'=>'','01'=>'Jan','02'=>'Fev','03'=>'Mar','04'=>'Abr','05'=>'Mai','06'=>'Jun','07'=>'Jul','08'=>'Ago','09'=>'Set','10'=>'Out','11'=>'Nov','12'=>'Dez'),array('0'=>'','01'=>'Janeiro','02'=>'Fevereiro','03'=>'Mar&ccedil;o','04'=>'Abril','05'=>'Maio','06'=>'Junho','07'=>'Julho','08'=>'Agosto','09'=>'Setembro','10'=>'Outubro','11'=>'Novembro','12'=>'Dezembro'));

                        $week_ext = array(array('1'=>'Seg','2'=>'Ter','3'=>'Qua','4'=>'Qui','5'=>'Sex','6'=>'Sab','0'=>'Dom'),array('1'=>'Segunda','2'=>'Ter&ccedil;a','3'=>'Quarta','4'=>'Quinta','5'=>'Sexta','6'=>'Sábado','0'=>'Domingo'));
                        $weekDay = date("w", mktime(0,0,0,$months,$day,$year) );
                        if($value['type'] == 1)
                        {
                            $return_date = $months_ext[1][$months].' '.$day.' de '.$year;
                        }
                        elseif($value['type']=='DW')
                        {
                            $return_date = $week_ext[1][$weekDay];
                        }
                        elseif($value['type']== 2)
                        {
                            $return_date = $day.'/'.$months.'/'.$year;
                        }
                        elseif($value['type']==3){
                            $return_date = $months_ext[1][$months].' '.$day.' de '.$year.' '.$time;
                        }
                    }
                }
            }
            return $return_date;
        }

        public function btnPagar($param = null,$dataPagamento=null){
            if($dataPagamento == null){
                $btnPagar = $this->Html->link('<i class="fa fa-money"></i>', 'javascript:;',array("class"=>"btn btn-primary btn-xs",'escape'=>false,"title"=>"Pagar",'id'=>'sa-btn-ContaReceber-Pagar'.$param['id'],'onclick'=>'pagarConta('.$param['id'].','.$param['Mes'].')','data-target'=>".bs-example-modal-lg" ));
            }else{
                $btnPagar = $this->Html->link('<i class="fa fa-money"></i>', 'javascript:;',array("class"=>"btn btn-success btn-xs",'escape'=>false,"title"=>"Estornar",'id'=>'sa-btn-ContaReceber-Pagar'.$param['id'],'onclick'=>'estornarConta('.$param['id'].','.$param['Mes'].')','data-target'=>".bs-example-modal-lg" ));
            }
            return $btnPagar;
        }

        public function emAtrazo($param=null){
            $this->mes_qtd_ContaAtrazo = '';
            $quantidade = '';
            if(isset($param)){
                if($param != null || $param != 0){
                    $quantidade = '<span class="badge badge-danger" title="'.$param.' conta(s) em Atraso(s)">'.$param.'</span>';
                }
            }
            return $quantidade;
        }

        public function btnSelecionaMes($param = null){

            $classActive = '';
            $btnClass = 'btn-white';
            $anoAtual = date('Y');
            $meses = array_merge(array(
                    "01/{$anoAtual}"=> null,
                    "02/{$anoAtual}"=> null,
                    "03/{$anoAtual}"=> null,
                    "04/{$anoAtual}"=> null,
                    "05/{$anoAtual}"=> null,
                    "06/{$anoAtual}"=> null,
                    "07/{$anoAtual}"=> null,
                    "08/{$anoAtual}"=> null,
                    "09/{$anoAtual}"=> null,
                    "10/{$anoAtual}"=> null,
                    "11/{$anoAtual}"=> null,
                    "12/{$anoAtual}"=> null,
                ),
                $param["mesAtrazados"]
            );

            $btn = array('<button type="button" onclick="listarContaMes(01)" class="btn btn-white col-lg-1">JAN '.$this->emAtrazo($meses['01/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(02)" class="btn btn-white col-lg-1">FEV '.$this->emAtrazo($meses['02/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(03)" class="btn btn-white col-lg-1">MAR '.$this->emAtrazo($meses['03/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(04)" class="btn btn-white col-lg-1">ABR '.$this->emAtrazo($meses['04/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(05)" class="btn btn-white col-lg-1">MAI '.$this->emAtrazo($meses['05/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(06)" class="btn btn-white col-lg-1">JUN '.$this->emAtrazo($meses['06/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(07)" class="btn btn-white col-lg-1">JUL '.$this->emAtrazo($meses['07/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(08)" class="btn btn-white col-lg-1">AGO '.$this->emAtrazo($meses['08/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(09)" class="btn btn-white col-lg-1">SET '.$this->emAtrazo($meses['09/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(10)" class="btn btn-white col-lg-1">OUT '.$this->emAtrazo($meses['10/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(11)" class="btn btn-white col-lg-1">NOV '.$this->emAtrazo($meses['11/'.$anoAtual]).'</button>',
                '<button type="button" onclick="listarContaMes(12)" class="btn btn-white col-lg-1">DEZ '.$this->emAtrazo($meses['12/'.$anoAtual]).'</button>');
            switch($param['mes']){
                case "01":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[0] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">JAN</button>';
                    break;
                case "02":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[1] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">FEV</button>';
                    break;
                case "03":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[2] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">MAR</button>';
                    break;
                case "04":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[3] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">ABR</button>';
                    break;
                case "05":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[4] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">MAI</button>';
                    break;
                case "06":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[5] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">JUN</button>';
                    break;
                case "07":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[6] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">JUL</button>';
                    break;
                case "08":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[7] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">AGO</button>';
                    break;
                case "09":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[8] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">SET</button>';
                    break;
                case "10":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[9] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">OUT</button>';
                    break;
                case "11":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[10] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">NOV</button>';
                    break;
                case "12":
                    $classActive = 'active';
                    $btnClass = 'btn-success';
                    $btn[11] = '<button type="button" class="btn '.$btnClass.' col-lg-1 '.$classActive.'">DEZ</button>';
                    break;
            }
            $btns = $btn[0].$btn[1].$btn[2].$btn[3].$btn[4].$btn[5].$btn[6].$btn[7].$btn[8].$btn[9].$btn[10].$btn[11];
            return $btns;
        }

    }